﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using ShanjinPDAUI.ModelEntity;

namespace ShanjinPDAUI.Data
{
    public class AGVDataServer
    {
        public async Task<IEnumerable<AGVData>> GetAGVDatas()
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get,
                  $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/AGVsData");
                    request.Headers.Add("Accept", "text / plain");
                    //var client = _httpClientFactory.CreateClient();
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var result = await JsonSerializer.DeserializeAsync
                                <IEnumerable<AGVData>>(responseStream);
                            return result;
                        }
                        else
                        {
                            return null;                        
                        }

                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
            }

        }
    }
}
